using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
    using System.Globalization;
	#endregion
	public partial class AdminController: BaseController
	{
        public ActionResult SelectOrders()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_ORDERS,
            };
            return View(CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SelectOrders_Update(FormCollection forms)
        {
            CultureInfo provider = new CultureInfo("en");
            var shippdate = forms.GetValues("Orders_NgayGiaoHang")[0];

            var orders = _repDonDatHang.SelectByID(int.Parse(forms.GetValues("Orders_ID")[0]));
            orders.DaXacNhan = bool.Parse(forms.GetValues("IsVerified")[0]);
            orders.DaGiaoHang = bool.Parse(forms.GetValues("IsShipped")[0]);
            orders.HuyDonHang = bool.Parse(forms.GetValues("IsCanceled")[0]);

            if (!string.Empty.Equals(shippdate))
            {
                orders.NgayGiaoHang = DateTime.Parse(shippdate, provider);
            }
            else
            {
                orders.NgayGiaoHang = null;
            }

            if (!orders.DaGiaoHang.Value)
            {
                orders.NgayGiaoHang = null;
            }

            var result = _repDonDatHang.Save();

            var data = new DataTransferViewModel
            {
                IDDonDatHang = orders.ID,
                EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL_EDIT,
            };
            return View("SelectOrders", CreateViewModel(data));
        }

        public ActionResult SelectOrders_Edit(string idorder)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL_EDIT,
                IDDonDatHang = int.Parse(idorder),
                IntID = int.Parse(idorder),
            };
            return View("SelectOrders_Edit", CreateViewModel(data));
        }

        public ActionResult SelectOrders_Cancel(string idorder)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL_EDIT,
                IDDonDatHang = int.Parse(idorder),
                IntID = int.Parse(idorder)
            };
            return View("SelectOrders", CreateViewModel(data));
        }

        public ActionResult SelectOrder_Detail(string idorder)
        {
            var data = new DataTransferViewModel
            {
                IDDonDatHang = int.Parse(idorder),
                EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL_EDIT,
            };
            return View("SelectOrders", CreateViewModel(data));
        }

        public ActionResult Revenue()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_REVENUE,
            };
            return View(CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Revenue(FormCollection forms)
        {
            CultureInfo provider = new CultureInfo("en");
            var from = forms.GetValues("Revenue_From")[0];
            var to = forms.GetValues("Revenue_To")[0];
            var data = new DataTransferViewModel
            {
                From = DateTime.Parse(from, provider),
                To = DateTime.Parse(to, provider),
                EnumViewModelType = EnumViewModel.ADMIN_ORDERS_REVENUE_EXECUTE,
            };
            return View("Revenue", CreateViewModel(data));
        }

        public ActionResult SelectOrderDetail(string idorder, string from, string to)
        {
            var data = new DataTransferViewModel
            {
                IDDonDatHang = int.Parse(idorder),
                From = DateTime.Parse(from),
                To = DateTime.Parse(to),
                EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
            };
            return View("Revenue", CreateViewModel(data));
        }

        public ActionResult EditOrders(string idorder, string from, string to)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
                IDDonDatHang = int.Parse(idorder),
                IntID = int.Parse(idorder),
                From = DateTime.Parse(from),
                To = DateTime.Parse(to),
            };
            return View("EditOrders", CreateViewModel(data));
        }

        public ActionResult CancelEditingOrders(string idorder, string from, string to)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
                IDDonDatHang = int.Parse(idorder),
                IntID = int.Parse(idorder),
                From = DateTime.Parse(from),
                To = DateTime.Parse(to),
            };
            return View("Revenue", CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateOrders(FormCollection forms)
        {
            CultureInfo provider = new CultureInfo("en");
            var from = forms.GetValues("Orders_From")[0];
            var to = forms.GetValues("Orders_To")[0];
            var shippdate = forms.GetValues("Orders_NgayGiaoHang")[0];

            var orders = _repDonDatHang.SelectByID(int.Parse(forms.GetValues("Orders_ID")[0]));
            orders.DaXacNhan = bool.Parse(forms.GetValues("IsVerified")[0]);
            orders.DaGiaoHang = bool.Parse(forms.GetValues("IsShipped")[0]);
            orders.HuyDonHang = bool.Parse(forms.GetValues("IsCanceled")[0]);

            if (!string.Empty.Equals(shippdate))
            {
                orders.NgayGiaoHang = DateTime.Parse(shippdate, provider);
            }
            else
            {
                orders.NgayGiaoHang = null;
            }

            if (!orders.DaGiaoHang.Value)
            {
                orders.NgayGiaoHang = null;
            }

            var result = _repDonDatHang.Save();

            var data = new DataTransferViewModel
            {
                IDDonDatHang = orders.ID,
                From = DateTime.Parse(from),
                To = DateTime.Parse(to),
                EnumViewModelType = EnumViewModel.ADMIN_ORDERDETAIL,
            };
            return View("Revenue", CreateViewModel(data));
        }

        public ActionResult DeleteOrder(int id, string from, string to)
        {
            CultureInfo provider = new CultureInfo("en");
            var deleteItem = _repDonDatHang.SelectByID(id);
            var result = _repDonDatHang.Delete(id);

            var data = new DataTransferViewModel
            {
                From = DateTime.Parse(from, provider),
                To = DateTime.Parse(to, provider),
                EnumViewModelType = EnumViewModel.ADMIN_ORDERS_REVENUE_EXECUTE,
            };
            return View("Revenue", CreateViewModel(data));
        }

        public ActionResult TimKiemDonHang()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_TIMKIEMDONHANG,
            };
            return View(CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TimKiemDonHang(FormCollection forms)
        {
            var apdung_daxacnhan = bool.Parse(forms.GetValues("DaXacNhan")[0]);
            var apdung_daxacnhan_val = bool.Parse(forms.GetValues("DaXacNhan_Value")[0]) ? "true" : "false";

            var apdung_dagiaohang = bool.Parse(forms.GetValues("DaGiaoHang")[0]);
            var apdung_dagiaohang_val = bool.Parse(forms.GetValues("DaGiaoHang_Value")[0]) ? "true" : "false";

            var apdung_dahuy = bool.Parse(forms.GetValues("DaHuy")[0]);
            var apdung_dahuy_val = bool.Parse(forms.GetValues("DaHuy_Value")[0]) ? "true" : "false";

            var data = new DataTransferViewModel
            {
                TimKiem_DonDatHang_IDKhachHang = forms.GetValues("IDKhachHang")[0],
                TimKiem_DonDatHang_DaXacNhan = apdung_daxacnhan ? apdung_daxacnhan_val : string.Empty,
                TimKiem_DonDatHang_DaGiaoHang = apdung_dagiaohang ? apdung_dagiaohang_val : string.Empty,
                TimKiem_DonDatHang_DaHuy = apdung_dahuy ? apdung_dahuy_val : string.Empty,
                TimKiem_DonDatHang_KhuVuc = forms.GetValues("KhuVuc")[0],
                EnumViewModelType = EnumViewModel.ADMIN_TIMKIEMDONHANG_EXECUTE,
            };

            ViewData["DaXacNhan"] = apdung_daxacnhan ? apdung_daxacnhan.ToString() : string.Empty;
            ViewData["DaGiaoHang"] = apdung_dagiaohang ? apdung_dagiaohang.ToString() : string.Empty;
            ViewData["DaHuy"] = apdung_dahuy ? apdung_dahuy.ToString() : string.Empty;
            ViewData["DaXacNhan_Value"] = apdung_daxacnhan_val;
            ViewData["DaGiaoHang_Value"] = apdung_dagiaohang_val;
            ViewData["DaHuy_Value"] = apdung_dahuy_val;
            ViewData["IDKhachHang"] = forms.GetValues("IDKhachHang")[0];
            ViewData["KhuVuc"] = forms.GetValues("KhuVuc")[0];

            return View(CreateViewModel(data));
        }

        public ActionResult TimKiem_SelectOrderDetail(string idorder, string idKhachHang, 
            string apdung_daxacnhan, string apdung_daxacnhan_val,
            string apdung_dagiaohang, string apdung_dagiaohang_val,
            string apdung_dahuy, string apdung_dahuy_val, 
            string khuvuc)
        {
            Guid idKhachHangVal = Guid.Parse("11112222-1111-1111-1111-111122223333");

            var data = new DataTransferViewModel
            {
                IDDonDatHang = int.Parse(idorder),
                TimKiem_DonDatHang_IDKhachHang = idKhachHang,
                TimKiem_DonDatHang_DaXacNhan = apdung_daxacnhan.Equals(string.Empty) ? string.Empty : apdung_daxacnhan_val,
                TimKiem_DonDatHang_DaGiaoHang = apdung_dagiaohang.Equals(string.Empty) ? string.Empty : apdung_dagiaohang_val,
                TimKiem_DonDatHang_DaHuy = apdung_dahuy.Equals(string.Empty) ? string.Empty : apdung_dahuy_val,
                TimKiem_DonDatHang_KhuVuc = khuvuc,
                EnumViewModelType = EnumViewModel.ADMIN_TIMKIEMDONHANG_SELECT,
            };

            ViewData["DaXacNhan"] = apdung_daxacnhan;
            ViewData["DaGiaoHang"] = apdung_dagiaohang;
            ViewData["DaHuy"] = apdung_dahuy;
            ViewData["DaXacNhan_Value"] = apdung_daxacnhan_val;
            ViewData["DaGiaoHang_Value"] = apdung_dagiaohang_val;
            ViewData["DaHuy_Value"] = apdung_dahuy_val;
            ViewData["IDKhachHang"] = idKhachHang;
            ViewData["KhuVuc"] = khuvuc;

            return View("TimKiemDonHang", CreateViewModel(data));
        }

        public ActionResult TimKiem_EditOrders(string idorder, string idKhachHang,
            string apdung_daxacnhan, string apdung_daxacnhan_val,
            string apdung_dagiaohang, string apdung_dagiaohang_val,
            string apdung_dahuy, string apdung_dahuy_val,
            string khuvuc)
        {
            Guid idKhachHangVal = Guid.Parse("11112222-1111-1111-1111-111122223333");
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_TIMKIEMORDERDETAIL,
                IDDonDatHang = int.Parse(idorder),
                IntID = int.Parse(idorder),
                TimKiem_DonDatHang_IDKhachHang = idKhachHang,
                TimKiem_DonDatHang_DaXacNhan = apdung_daxacnhan.Equals(string.Empty) ? string.Empty : apdung_daxacnhan_val,
                TimKiem_DonDatHang_DaGiaoHang = apdung_dagiaohang.Equals(string.Empty) ? string.Empty : apdung_dagiaohang_val,
                TimKiem_DonDatHang_DaHuy = apdung_dahuy.Equals(string.Empty) ? string.Empty : apdung_dahuy_val,
                TimKiem_DonDatHang_KhuVuc = khuvuc,
            };

            ViewData["DaXacNhan"] = apdung_daxacnhan;
            ViewData["DaGiaoHang"] = apdung_dagiaohang;
            ViewData["DaHuy"] = apdung_dahuy;
            ViewData["DaXacNhan_Value"] = apdung_daxacnhan_val;
            ViewData["DaGiaoHang_Value"] = apdung_dagiaohang_val;
            ViewData["DaHuy_Value"] = apdung_dahuy_val;
            ViewData["IDKhachHang"] = idKhachHang;
            ViewData["KhuVuc"] = khuvuc;

            return View("TimKiemEditOrders", CreateViewModel(data));
        }

        public ActionResult TimKiem_CancelEditingOrders(string idorder, string idKhachHang,
            string apdung_daxacnhan, string apdung_daxacnhan_val,
            string apdung_dagiaohang, string apdung_dagiaohang_val,
            string apdung_dahuy, string apdung_dahuy_val,
            string khuvuc)
        {
            Guid idKhachHangVal = Guid.Parse("11112222-1111-1111-1111-111122223333");
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_TIMKIEMORDERDETAIL,
                IDDonDatHang = int.Parse(idorder),
                IntID = int.Parse(idorder),
                TimKiem_DonDatHang_IDKhachHang = idKhachHang,
                TimKiem_DonDatHang_DaXacNhan = apdung_daxacnhan.Equals(string.Empty) ? string.Empty : apdung_daxacnhan_val,
                TimKiem_DonDatHang_DaGiaoHang = apdung_dagiaohang.Equals(string.Empty) ? string.Empty : apdung_dagiaohang_val,
                TimKiem_DonDatHang_DaHuy = apdung_dahuy.Equals(string.Empty) ? string.Empty : apdung_dahuy_val,
                TimKiem_DonDatHang_KhuVuc = khuvuc
            };

            ViewData["DaXacNhan"] = apdung_daxacnhan;
            ViewData["DaGiaoHang"] = apdung_dagiaohang;
            ViewData["DaHuy"] = apdung_dahuy;
            ViewData["DaXacNhan_Value"] = apdung_daxacnhan_val;
            ViewData["DaGiaoHang_Value"] = apdung_dagiaohang_val;
            ViewData["DaHuy_Value"] = apdung_dahuy_val;
            ViewData["IDKhachHang"] = idKhachHang;
            ViewData["KhuVuc"] = khuvuc;

            return View("TimKiemDonHang", CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TimKiem_UpdateOrders(FormCollection forms)
        {
            CultureInfo provider = new CultureInfo("en");
            var shippdate = forms.GetValues("Orders_NgayGiaoHang")[0];

            var orders = _repDonDatHang.SelectByID(int.Parse(forms.GetValues("Orders_ID")[0]));
            orders.DaXacNhan = bool.Parse(forms.GetValues("IsVerified")[0]);
            orders.DaGiaoHang = bool.Parse(forms.GetValues("IsShipped")[0]);
            orders.HuyDonHang = bool.Parse(forms.GetValues("IsCanceled")[0]);

            if (!string.Empty.Equals(shippdate))
            {
                orders.NgayGiaoHang = DateTime.Parse(shippdate, provider);
            }

            if (!orders.DaGiaoHang.Value)
            {
                orders.NgayGiaoHang = null;
            }

            var result = _repDonDatHang.Save();

            // Get thong tin tim kiem
            var apdung_daxacnhan = forms.GetValues("DaXacNhan")[0];
            var apdung_daxacnhan_val = forms.GetValues("DaXacNhan_Value")[0];

            var apdung_dagiaohang = forms.GetValues("DaGiaoHang")[0];
            var apdung_dagiaohang_val = forms.GetValues("DaGiaoHang_Value")[0];

            var apdung_dahuy = forms.GetValues("DaHuy")[0];
            var apdung_dahuy_val = forms.GetValues("DaHuy_Value")[0];

            var data = new DataTransferViewModel
            {
                IDDonDatHang = orders.ID,
                IntID = orders.ID,
                EnumViewModelType = EnumViewModel.ADMIN_TIMKIEMORDERDETAIL,
                TimKiem_DonDatHang_IDKhachHang = forms.GetValues("IDKhachHang")[0],
                TimKiem_DonDatHang_DaXacNhan = apdung_daxacnhan.Equals(string.Empty) ? string.Empty : apdung_daxacnhan_val,
                TimKiem_DonDatHang_DaGiaoHang = apdung_dagiaohang.Equals(string.Empty) ? string.Empty : apdung_dagiaohang_val,
                TimKiem_DonDatHang_DaHuy = apdung_dahuy.Equals(string.Empty) ? string.Empty : apdung_dahuy_val,
                TimKiem_DonDatHang_KhuVuc = forms.GetValues("KhuVuc")[0],
            };

            ViewData["DaXacNhan"] = apdung_daxacnhan;
            ViewData["DaGiaoHang"] = apdung_dagiaohang;
            ViewData["DaHuy"] = apdung_dahuy;
            ViewData["DaXacNhan_Value"] = apdung_daxacnhan_val;
            ViewData["DaGiaoHang_Value"] = apdung_dagiaohang_val;
            ViewData["DaHuy_Value"] = apdung_dahuy_val;
            ViewData["IDKhachHang"] = forms.GetValues("IDKhachHang")[0];
            ViewData["KhuVuc"] = forms.GetValues("KhuVuc")[0];

            return View("TimKiemDonHang", CreateViewModel(data));
        }
	}
}